from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from models import *
import hashlib, json

def department(request):
    errors = []
    if 'op' in request.POST:
        if request.POST['op'] == 'add':
            name = request.POST.get('dp_name')
            addr = request.POST.get('dp_addr')
            if None in (name,addr) or name == '' or addr == '':
                errors.append("enter the Name and Address for Department")
            else:
                if len(Department.objects.filter(name=name)) == 0:
                    Department(name=name, address=addr).save()
                else:
                    Department.objects.filter(name=name).update(address=addr)
        elif request.POST['op'] == 'del':
            name = request.POST.get('dp_del')
            if name == None or name == '':
                errors.append("select a department for delete")
            else:
                Department.objects.get(name=request.POST['dp_del']).delete()
        else:
            errors.append("give a oprate?")
    dp_list = Department.objects.all()
    return  render_to_response('diary/department_list.html', locals(), context_instance=RequestContext(request))

def employee(request):
    errors = []
    if 'op' in request.POST:
        if request.POST['op'] == 'add':
            name = request.POST.get('ey_name')
            passwd = request.POST.get('ey_passwd')
            sex = request.POST.get('ey_sex')
            birthday = request.POST.get('ey_birthday')
            department = request.POST.get('ey_department')
            email = request.POST.get('ey_email')
            if None in (name, passwd, sex, birthday, department, email) or \
                    '' in (name, passwd, sex, birthday, department, email):
                errors.append("enter the field of the employee")
            else:
                dp = Department.objects.get(name=department)
                passwd = hashlib.md5(passwd).hexdigest()
                Employee(name=name, passwd=passwd, sex=sex, birthday=birthday, department=dp, email=email).save()
        elif reuqest.POST['op'] == 'del':
            name = request.POST.get('ey_del')
            if name == None or name == '':
                errors.append("select a employee for delete")
            else:
                Employee.objects.get(name=name).delete()
        else:
            errors.append("give a oprate?")
    ey_list = Employee.objects.all()
    dp_list = Department.objects.all()
    return render_to_response('diary/employee_list.html', locals(), context_instance=RequestContext(request))

def project(request):
    errors = []
    print request.POST
    if 'op' in request.POST:
        if request.POST['op'] == 'add':
            name = request.POST.get('pj_name')
            memo = request.POST.get('pj_memo')
            startdate = request.POST.get('pj_startdate')
            member = request.POST.getlist('pj_member')
            if None in (name, memo, startdate, member) or \
                    '' in (name, memo, startdate, member):
                errors.append("enter the field of the project")
            else:
                p = Project.objects.filter(name=name)
                if len(p) == 0:
                    p = Project(name=name, memo=memo, startdate=startdate)
                    p.save()
                else:
                    p = p[0]
                for mb in member:
                    try:
                        ey = Employee.objects.get(name=mb)
                        p.member.add(ey)
                    except:
                        error.append("add %s to project error"%mb)
        elif request.POST['op'] == 'del':
            name = request.POST.get('pj_del')
            if name == None or name == '':
                errors.append("select a project for delete")
            else:
                Project.objects.get(name=name).delete()
        else:
            errors.append("give a oprate?")
    pj_list = Project.objects.all()
    ey_list = Employee.objects.all()
    return render_to_response('diary/project_list.html', locals(), context_instance=RequestContext(request))

def task(request):
    errors = []
    if 'op' in request.POST:
        if request.POST['op'] == 'add':
            name = request.POST.get('tk_name')
            user = request.POST.get('tk_user')
            project = request.POST.get('tk_project')
            plan_start_date = request.POST.get('tk_plan_start_date')
            plan_finish_date = request.POST.get('tk_plan_finish_date')
            actual_start_date = request.POST.get('tk_actual_start_date')
            actual_finish_date = request.POST.get('tk_actual_finish_date')
            task_info = request.POST.get('tk_task_info')
            if None in (name, user, project, plan_start_date, plan_finish_date, actual_start_date, actual_finish_date, task_info) or \
                    '' in (name, user, project, plan_start_date):
                errors.append("enter the field of the task")
            else:
                if len(Task.objects.filter(name=name)) == 0:
                    ey = Employee.objects.get(name=user)
                    pj = Project.objects.get(name=project)
                    if plan_finish_date == '':
                        plan_finish_date = None
                    if actual_start_date == '':
                        actual_start_date = None
                    if actual_finish_date == '':
                        actual_finish_date = None
                    Task(name=name, user=ey, project=pj, \
                            plan_start_date=plan_start_date, plan_finish_date=plan_finish_date, \
                            actual_start_date=actual_start_date, actual_finish_date=actual_finish_date, \
                            task_info=task_info).save()
        elif request.POST['op'] == 'del':
            name = request.POST.get('tk_del')
            if name == None or name == '':
                errors.append("select a task for delete")
            else:
                Task.objects.get(name=name).delete()
        else:
            errors.apped("give a operate?")
    tk_list = Task.objects.all()
    pj_list = Project.objects.all()
    ey_list = Employee.objects.all()
    return render_to_response('diary/task_list.html', locals(), context_instance=RequestContext(request))



def task_json(request):
    m = []
    for i in Task.objects.all():
        x = {}
        x['title'] = '%s@%s'%(i.name, i.user)
        x['start'] = i.plan_start_date.isoformat()
        if i.plan_finish_date != None:
            x['end'] = i.plan_finish_date.isoformat()
        m.append(x)
    return HttpResponse(json.dumps(m), mimetype='application/javascript')
